﻿using CarPCBATest.Procedure;
using Common;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace CarPCBATest.ViewModel
{
    internal class DebugViewModel : NotifyBase
    {
        private bool loaded = false;
        private bool _ioPower;

        /// <summary>
        /// 产品供电
        /// </summary>
        public bool IOPower
        {
            get { return _ioPower; }
            set
            {
                _ioPower = value;
                IOModule.SetDOValue(DODefine.Power, value ? 1 : 0);
                this.DoNotify();
            }
        }

        private bool _ioOKLed;

        /// <summary>
        /// 绿灯
        /// </summary>
        public bool IOOkLed
        {
            get { return _ioOKLed; }
            set
            {
                _ioOKLed = value;
                IOModule.SetDOValue(DODefine.OK, value ? 1 : 0);

                this.DoNotify();
            }
        }

        private bool _ioNgLed;

        /// <summary>
        /// 报警灯
        /// </summary>
        public bool IONgLed
        {
            get { return _ioNgLed; }
            set
            {
                _ioNgLed = value;
                IOModule.SetDOValue(DODefine.NG, value ? 1 : 0);

                this.DoNotify();
            }
        }

        private bool _ioAir;

        /// <summary>
        /// 气缸下压
        /// </summary>
        public bool IOAir
        {
            get { return _ioAir; }
            set
            {
                _ioAir = value;
                IOModule.SetDOValue(DODefine.AirReset, value ? 1 : 0);

                this.DoNotify();
            }
        }

        private bool _ioLineSwitch;

        /// <summary>
        /// 高电平烧录低电平测试
        /// </summary>
        public bool IOLineSwitch
        {
            get { return _ioLineSwitch; }
            set
            {
                _ioLineSwitch = value;
                IOModule.SetDOValue(DODefine.LineSwitch, value ? 1 : 0);

                this.DoNotify();
            }
        }

        private bool _ioSafe;

        /// <summary>
        /// 安全光幕
        /// </summary>
        public bool IOSafe
        {
            get { return _ioSafe; }
            set
            {
                _ioSafe = value;
                this.DoNotify();
            }
        }

        private bool _ioStartButton;

        /// <summary>
        /// 启动按钮
        /// </summary>
        public bool IOStartButton
        {
            get { return _ioStartButton; }
            set
            {
                _ioStartButton = value;
                this.DoNotify();
            }
        }

        private bool _ioEmgStop;

        /// <summary>
        /// 急停按钮
        /// </summary>
        public bool IOEmgStop
        {
            get { return _ioEmgStop; }
            set
            {
                _ioEmgStop = value;
                this.DoNotify();
            }
        }

        private bool _ioResetButtton;

        /// <summary>
        /// 复位按钮
        /// </summary>
        public bool IOResetButton
        {
            get { return _ioResetButtton; }
            set
            {
                _ioResetButtton = value;
                this.DoNotify();
            }
        }

        private bool _ioAirWorkSenser;

        /// <summary>
        /// 气缸到位
        /// </summary>
        public bool IOAirWorkSenser
        {
            get { return _ioAirWorkSenser; }
            set
            {
                _ioAirWorkSenser = value;
                this.DoNotify();
            }
        }

        private string _current;

        /// <summary>
        /// 电流表电流
        /// </summary>
        public string Current
        {
            get { return _current; }
            set
            {
                _current = value;
                this.DoNotify();
            }
        }

        private double _power;

        /// <summary>
        /// 电压表设置电压
        /// </summary>
        public double Power
        {
            get { return _power; }
            set
            {
                _power = value;
                this.DoNotify();
            }
        }

        private int _selectedChannel;

        /// <summary>
        /// 电压表通道
        /// </summary>
        public int SelectedChannel
        {
            get { return _selectedChannel; }
            set
            {
                _selectedChannel = value;
                this.DoNotify();
            }
        }

        private double _current2;

        /// <summary>
        /// 电压表电流
        /// </summary>
        public double Current2
        {
            get { return _current2; }
            set
            {
                _current2 = value;
                this.DoNotify();
            }
        }

        public CommandBase GetCurrentCommand { get; set; }
        public CommandBase SetPowerCommand { get; set; }
        public CommandBase SetCurrentCommand { get; set; }
        public CommandBase UnloadedCommand { get; set; }
        public CommandBase LoadedCommand { get; set; }
        public CommandBase SetRemoteCommand { get; set; }
        public CommandBase OnChannelCommand { get; set; }
        public CommandBase OffChannelCommand { get; set; }

        public DebugViewModel()
        {
            try
            {
                SelectedChannel = 1;
                GetCurrentCommand = new CommandBase(GetCurrent);
                SetPowerCommand = new CommandBase(SetPower);
                SetCurrentCommand = new CommandBase(SetCurrent);
                UnloadedCommand = new CommandBase(Unloaded);
                LoadedCommand = new CommandBase(Loaded);
                SetRemoteCommand = new CommandBase(SetRemote);
                OnChannelCommand = new CommandBase(OnChannel);
                OffChannelCommand = new CommandBase(OffChannel);
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                var mwin = Application.Current.MainWindow as MetroWindow;

                mwin.ShowMessageAsync("错误", ex.Message);
            }
        }

        private void OffChannel(object obj)
        {
            try
            {
                IT6302A.SetChannelOutput(SelectedChannel, false);
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                var mwin = Application.Current.MainWindow as MetroWindow;

                mwin.ShowMessageAsync("错误", ex.Message);
            }
        }

        private void OnChannel(object obj)
        {
            try
            {
                IT6302A.SetChannelOutput(SelectedChannel, true);
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                var mwin = Application.Current.MainWindow as MetroWindow;

                mwin.ShowMessageAsync("错误", ex.Message);
            }
        }

        private void SetRemote(object obj)
        {
            try
            {
                IT6302A.SwitchRemote();
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                var mwin = Application.Current.MainWindow as MetroWindow;

                mwin.ShowMessageAsync("错误", ex.Message);
            }
        }

        private void Loaded(object obj)
        {
            try
            {
                DM3058E.Connect();
                IT6302A.Connect();

                IOModule.Connect();
                Thread.Sleep(100);
                loaded = true;
                Task.Run(() =>
                {
                    while (loaded)
                    {
                        IOStartButton = IOModule.GetDIValue(DIDefine.Start);
                        IOResetButton = IOModule.GetDIValue(DIDefine.Reset);
                        IOSafe = IOModule.GetDIValue(DIDefine.SafeLight);
                        IOAirWorkSenser = IOModule.GetDIValue(DIDefine.AirWork);
                        IOEmgStop = IOModule.GetDIValue(DIDefine.EmgStop);

                        IOOkLed = IOModule.GetDOValue(DODefine.OK);
                        IOPower = IOModule.GetDOValue(DODefine.Power);
                        IOAir = IOModule.GetDOValue(DODefine.AirReset);
                        IONgLed = IOModule.GetDOValue(DODefine.NG);
                        IOLineSwitch = IOModule.GetDOValue(DODefine.LineSwitch);
                        Thread.Sleep(500);
                    }
                });
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                var mwin = Application.Current.MainWindow as MetroWindow;

                mwin.ShowMessageAsync("错误", ex.Message);
            }
        }

        private void Unloaded(object obj)
        {
            loaded = false;
        }

        private void SetCurrent(object obj)
        {
            try
            {
                IT6302A.SetCurrent(SelectedChannel, Current2);
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                var mwin = Application.Current.MainWindow as MetroWindow;

                mwin.ShowMessageAsync("错误", ex.Message);
            }
        }

        private void SetPower(object obj)
        {
            try
            {
                IT6302A.SetVoltage(SelectedChannel, Power);
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                var mwin = Application.Current.MainWindow as MetroWindow;

                mwin.ShowMessageAsync("错误", ex.Message);
            }
        }

        private void GetCurrent(object obj)
        {
            try
            {
                var tmp = DM3058E.GetCurrent();
                if (Math.Abs(tmp) > 1)
                    Current = tmp + "A";
                else if (Math.Abs(tmp * 1000) > 1)
                    Current = tmp * 1000 + "mA";
                else if (Math.Abs(tmp * 1000000) > 1)
                    Current = tmp * 1000000 + "μA";
                else
                {
                    Current = tmp + "A";
                }
            }
            catch (Exception ex)
            {
                Mylog.Error(ex, ex.Message);
                var mwin = Application.Current.MainWindow as MetroWindow;

                mwin.ShowMessageAsync("错误", ex.Message);
            }
        }
    }
}